package _468_Validate_IP_Address;

/**
 * Created by 孙加辉 on 2018/7/22.
 */
class Solution {
    public String validIPAddress(String IP) {
        String res = "Neither";
        if(IP.length()<7)
            return res;
        if(IP.contains(".")){
            if (IP.startsWith(".")||IP.endsWith("."))
                return res;
            String[] ips = IP.split("\\.");
            if(ips.length!=4)
                return res;
            for(int i=0;i<4;i++){
                if(!isV4Part(ips[i]))
                    return  res;
            }
            res = "IPv4";
        }else if(IP.contains(":")){
            if (IP.startsWith(":")||IP.endsWith(":"))
                return res;
            String[] ips = IP.split(":");
            if(ips.length!=8)
                return res;
            for(int i=0;i<8;i++){
                if(!isV6Part(ips[i]))
                    return  res;
            }

            res = "IPv6";
        }
        return res;
    }
    public boolean isV4Part(String s){
        if(s.length()==0||s.length()>3)
            return false;
        if(s.startsWith("0")&&s.length()>1)
            return false;
        String regex = "^[0-9]{1,3}$";
        if(!s.matches(regex))
            return false;
        try {
            int sInt = Integer.parseInt(s);
            if(sInt>255||sInt<0)
                return false;
        }catch (Exception e){
            return false;
        }
        return true;
    }
    public boolean isV6Part(String s){
        String regex = "^[a-f0-9A-F]{1,4}$";
        return s.matches(regex);
    }
}
public class _468_Validate_IP_Address {
    public static void main(String[] args){
        Solution solution = new Solution();
       // String[] res = "2001:0db8:85a3:0:0:8A2E:0370:7334:".split(":");
        //System.out.println(res.length);
        System.out.println(solution.validIPAddress("192.0.0.1"));
    }
}
